package com.baizan.daka.ui.activity

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.text.InputType
import android.view.View
import android.widget.ArrayAdapter
import android.widget.SpinnerAdapter
import com.baizan.base.BaseActivity
import com.baizan.daka.mvp.model.bean.QuestionBean
import com.baizan.daka.mvp.model.bean.RegistInfoBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.RegisterContract
import com.baizan.daka.ui.dialog.ButtomDialog
import com.baizan.daka.ui.presenter.RegisterPresenter
import com.baizan.daka.util.StringMatches
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_register.*

class RegisterActivity : BaseActivity<RegisterPresenter>(), RegisterContract.View {
    var type: Int? = 0

    companion object {
        fun startFind(context: Context) {
            val intent = Intent(context, RegisterActivity::class.java)
            intent.putExtra("type", 1)
            context.startActivity(intent)
        }
    }

    var questions: List<QuestionBean>? = null
    var index: Int? = null
    override fun initData() {

        password_et.isPressed = true
        submit_bt.setOnClickListener {
            questions ?: let {
                showToast("密保问题获取失败")
                return@setOnClickListener
            }
            var account = account_et.text;
            if (account.isEmpty()) {
                showToast("请输入手机号码")
                return@setOnClickListener
            }
            var password = password_et.text
            if (password.isEmpty()) {
                showToast("请输入密码")
                return@setOnClickListener
            }
            if(!StringMatches.matchesPassword(password.toString())){
                showToast("请输入8-16位数字加字母的密码")
                return@setOnClickListener
            }
            var answerStr = answer.text;
            if (answerStr.isEmpty()) {
                showToast("请输入密保答案")
                return@setOnClickListener
            }
            var invitationCode = invitation_code_et.text
//            if (invitationCode.isEmpty()) {
//                showToast("请输入邀请码")
//                return@setOnClickListener
//            }
            var questionstr = question.text
            if (questionstr.isEmpty() or (index == null)) {
                showToast("请选择密保问题")
                return@setOnClickListener
            }
            if (type == 0) {
                presenter?.regist(
                    account.toString(),
                    password.toString(),
                    invitationCode.toString(),
                    questions!![index!!].questionCode,
                    questionstr.toString(),
                    answerStr.toString()
                )
            } else {
                presenter?.findPassword(
                    account.toString(),
                    password.toString(),
                    questions!![index!!].questionCode,
                    questionstr.toString(),
                    answerStr.toString()
                )
            }

        }
        eye_iv.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected)
                password_et.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            else
                password_et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

        }

    }

    override fun initView() {
        type = intent.getIntExtra("type", 0);

        action_bar.setRightOnClickListener(View.OnClickListener {
            finish()
        })
        question.setOnClickListener {
            questions?.let {
                showQuestion(it)
            }
        }
        if (type == 0) {
            action_bar.setCenterText("马上注册")
            action_bar.setLeftShow(false)
        } else {
            action_bar.setLeftShow(true)
            action_bar.setRightShow(false)
            action_bar.setCenterText("找回密码")
            invitation_code_et.visibility = View.GONE
            line4.visibility = View.GONE
            submit_bt.text = "确认"
        }
    }

    fun showQuestion(data: List<QuestionBean>) {
        val title: ArrayList<String> = ArrayList()
        for (ques in data) {
            title.add(ques.questionName)
        }
        val builder: ButtomDialog.Builder = ButtomDialog.Builder(this)
        builder.setContentData(title.toTypedArray())
        builder.setListener(object : ButtomDialog.OnItemSelectListener {

            override fun OnItemSelected(dialog: Dialog, position: Int, title: String?) {
                when (position) {
                    title?.length -> dialog.dismiss()
                    else -> {
                        question.text = title
                        index = position
                        dialog.dismiss()
                    }
                }
            }
        })
        builder.Build().show()
    }

    override fun start() {
        presenter?.getQuestionList()
    }

    override fun onDestroy() {
        super.onDestroy()

    }

    override fun registSuccess(code: String) {
        finish()
    }

    override fun sendSuccess(registInfo: RegistInfoBean) {
//        sendTime = registInfo.effectiveTime
        showToast("发送成功")
    }

    override fun getQuestionListSuccess(data: List<QuestionBean>) {
        if (!data.isEmpty() and (data.size > 0)) {
            question.setText(data[0].questionName)
            index = 0;
        }
        questions = data
    }

    override fun findSuccess() {
        showToast("成功找回密码")
        finish()
    }

    override fun initPresenter(): RegisterPresenter {
        return RegisterPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_register
    }
}